package cn.haulon.express.code;

import org.apache.commons.collections4.map.MultiKeyMap;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by Haulon on 18/09/2017.
 * <p>
 * Used as ...
 */
public abstract class AbstractExpressCode implements ExpressCode {
    private static final MultiKeyMap<String, String> expressCodeMap = new MultiKeyMap<>();

    @Override
    public String getName(String code) {
        return expressCodeMap.get(getServerKey(), code);
    }

    @Override
    public List<Map<String, String>> getCodes() {
        String serverKey = getServerKey();
        return expressCodeMap.entrySet().stream()
                .filter(multiKeyStringEntry -> multiKeyStringEntry.getKey().getKey(0).equals(serverKey))
                .map(multiKeyStringEntry -> {
                    Map<String, String> map = new HashMap<>();
                    map.put("code", multiKeyStringEntry.getKey().getKey(1));
                    map.put("name", multiKeyStringEntry.getValue());
                    return map;
                }).collect(Collectors.toList());
    }

    @Override
    public void set(String code, String name) {
        expressCodeMap.put(getServerKey(), code, name);
    }

    protected abstract String getServerKey();
}
